//
//  SpecialCollectionReusableView.m
//  B项目
//
//  Created by lanou on 15/12/18.
//  Copyright © 2015年 tzf. All rights reserved.
//

#import "SpecialCollectionReusableView.h"

@interface SpecialCollectionReusableView () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@end


@implementation SpecialCollectionReusableView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setCollectionView2]; //  设置collectionView相关
        _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(10, 150, self.frame.size.width - 10, 10))];
        _titleLabel.font = [UIFont systemFontOfSize:12];
        //        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _imageV = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 130, self.frame.size.width, 10))];
        _imageV.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
        [self addSubview:_imageV];
        [self addSubview:_titleLabel];
        
        
        UILabel *label = [[UILabel alloc] initWithFrame:(CGRectMake(10, 10, 80, 10))];
        label.text = @"专题";
        label.font = [UIFont systemFontOfSize:12];
        [self addSubview:label];
        
        //    UIButton *button = [[UIButton alloc] initWithFrame:(CGRectMake(kWidth - 80, 0, 80, 10))];
        _specialMoreButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
        _specialMoreButton.frame = CGRectMake(kWidth - 80, 10, 80, 10);
        [_specialMoreButton setTitle:@"更多专题>>" forState:(UIControlStateNormal)];
        [self addSubview:_specialMoreButton];
        _specialMoreButton.tintColor = [UIColor blackColor];
        
        
    }
    return self;
}



-(void)setTitle:(NSString *)title
{
    _titleLabel.text = title;
}






- (void)setCollectionView2
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //    flowLayout.minimumInteritemSpacing = 20;
    //    flowLayout.minimumLineSpacing = 20;
    // 分区内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 15, 10, 10);
    
    // 注意: item 的宽高必须要提前算好.
    flowLayout.itemSize = CGSizeMake(160, 80);
    
    //滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    _collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    
    //设置数据源和代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];
    _collectionView.frame = CGRectMake(0, 20, kWidth, 120);
    _collectionView.contentSize = CGSizeMake(kWidth * 3, 120);
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.pagingEnabled = YES;
    
    [self addSubview:_collectionView];
    
}


#pragma mark 代理相关
// 分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}

// 分区中 item 元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    // 默认为1个分区
    return 1;
}


// 加载单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UICollectionViewCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"mycell" forIndexPath:indexPath];
//    cell2.backgroundColor = [UIColor redColor];
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[_modelArray[indexPath.section] imageIcon]];
    imageV.layer.cornerRadius = 5;
    imageV.layer.masksToBounds = YES;
    cell2.backgroundView = imageV;
    
    return cell2;
    
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainClassTableViewController *mainClassTVC = [[MainClassTableViewController alloc] init];
    mainClassTVC.ID = [_modelArray[indexPath.section] ID];
    
    [mainClassTVC setHidesBottomBarWhenPushed:YES];
    mainClassTVC.navigationItem.title = [_modelArray[indexPath.section] title];
    mainClassTVC.type = @"Special";
    _myBlock(mainClassTVC);
}




@end
